package cn.schoolwow.sdk.video;

import cn.schoolwow.sdk.video.analyzer.Analyzer;
import cn.schoolwow.sdk.video.analyzer.BiliBiliAnalyzer;
import cn.schoolwow.sdk.video.analyzer.MGTVAnalyzer;
import cn.schoolwow.sdk.video.analyzer.YouKuAnalyzer;
import cn.schoolwow.sdk.video.domain.PlayList;
import cn.schoolwow.sdk.video.domain.VideoPart;

import java.io.IOException;

public class VideoUtil {
    private static Analyzer[] analyzers = new Analyzer[]{
            new BiliBiliAnalyzer(),
            new MGTVAnalyzer(),
            new YouKuAnalyzer(),
    };

    /**
     * 解析视频
     * @param url 视频链接
     * */
    public static PlayList getPlaylist(String url) throws IOException{
        for(Analyzer analyzer:analyzers){
            if(url.contains(analyzer.tv().domain)){
                return analyzer.getPlaylist(url);
            }
        }
        throw new IllegalArgumentException("不支持的播单地址!url:"+url);
    }

    /**
     * 解析并返回视频下载信息
     * */
    public static VideoPart[] getVideoPartList(String url) throws IOException{
        for(Analyzer analyzer:analyzers){
            if(url.contains(analyzer.tv().domain)){
                return analyzer.getVideoPartList(url);
            }
        }
        throw new IllegalArgumentException("不支持的播单地址!url:"+url);
    }

    /**
     * 转义文件名
     * */
    public static String escapeFileName(String fileName){
        StringBuilder builder = new StringBuilder(fileName);
        for(int i=0;i<builder.length();i++){
            if(builder.charAt(i)=='\\'
                    ||builder.charAt(i)=='/'
                    ||builder.charAt(i)==':'
                    ||builder.charAt(i)=='*'
                    ||builder.charAt(i)=='?'
                    ||builder.charAt(i)=='|'
                    ||builder.charAt(i)=='"'
                    ||builder.charAt(i)=='<'
                    ||builder.charAt(i)=='>'
            ){
                builder.setCharAt(i,'-');
            }
        }
        return builder.toString();
    }
}